package com.yifi.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.DBHelper;
import com.yifi.common.SysUtil;
import com.yifi.common.utils.DateUtils;
import com.yifi.common.utils.ToolSession;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;

/**
 * 
 * @ClassName: VideoService
 * @Description: 监控视频获取封面图片
 * @author 王浩斌
 * @date 2021-12-15 8:51:27
 *
 */

public  class VideoService {

	public static void Run() {
		String accessToken = SysUtil.get_ys_token();

		PropKit.use("jdbc.properties");
		String httpVideo = PropKit.get("config.video.url").toString();

		String sql = " select t1.* from sys_jk  t1 where   t1.cflag='1'   order by t1.cnum";
		List<Record> list_jk = Db.find(sql);
		for (Record rec : list_jk) {

			String id=rec.getStr("id");
			String deviceSerial=rec.getStr("c_sb");
			String channelNo = rec.getStr("cnum");

			String capture_img=rec.getStr("capture_img");
			Date capture_time=rec.getDate("capture_time");		//有效时间
			if(capture_time==null|| DateUtil.compare(capture_time, DateUtils.getNowDate())<=0)
			{

				HashMap<String, Object> paramMap = new HashMap<>();
				paramMap.put("accessToken",accessToken);
				paramMap.put("deviceSerial",deviceSerial);
				paramMap.put("channelNo",channelNo);

				String result= HttpUtil.post(httpVideo+"/lapp/device/capture", paramMap);
				JSONObject json=JSONObject.parseObject(result);
				if(json.get("code").toString().equals("200"))
				{
					capture_img=JSONObject.parseObject(json.get("data").toString()).get("picUrl").toString();
					String d1=DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", DateUtils.addHours(DateUtils.getNowDate(), 1));
					sql="update sys_jk set capture_img='"+capture_img+"',capture_time='"+d1+"' where id='"+id+"'";

					Db.update(sql);
				}
				rec.set("src",capture_img);
			}
			else
			{
				rec.set("src",capture_img);
			}

		}
	}
}
